package chapter08;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.TemporalField;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleTest {
    // 实现让每周四 18：00：00定时执行任务
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        LocalDateTime now = LocalDateTime.now();
        // 获取当前周的周四时间
        LocalDateTime thursday = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
        // 如果当前时间大于当前周四时间
        if (now.isAfter(thursday)){
            thursday=thursday.plusWeeks(1);
        }
        // 1）计算距离多少时间开始计算
        long initailDelay= Duration.between(now,thursday).toMillis();
        // 2）每隔多少小时执行一次
        long period=1000*60*60*24*7;
        pool.scheduleAtFixedRate(()->{
            System.out.println("do a running");
        },initailDelay,period, TimeUnit.MILLISECONDS);
    }
}
